<?php

namespace addons\yungift\controller;

use addons\yungift\library\Constant;
use addons\yungift\library\Logic;
use addons\yungift\model\Activity;
use addons\yungift\model\Member;
use app\common\controller\Api;

/**
 * Api 基础
 */
class BaseApi extends Api
{
    protected $noNeedRight = ['*'];

    protected $platform;
    protected $memberId;
    protected $memberData;
    protected $activityId;
    protected $activityData;

    /**
     * 初始化操作
     * @access protected
     */
    protected function _initialize()
    {
        parent::_initialize();

        // 跨域请求检测
        check_cors_request();

        $this->platform = input('get.platform');

        if ($this->auth->isLogin()) {
            $member = Member::where(['user_id' => $this->auth->id])->order('login_time desc')->find();
            if ($member && $member->status != Member::STATUS_NORMAL) {
                Member::update(['user_id' => 0], ['id' => $member->id]);
            }
            // 数据不全，强制下线
            if (! $member || $member->status != Member::STATUS_NORMAL) {
                $this->auth->logout();
                $this->error(__('登录失效，请重新登录'), null, Constant::INVALID_ACTIVITY);
            }
            $this->memberId = $member->id;
            $this->memberData = $member;
            if (! $activity = Activity::getCurrent($member)) {
                // 数据不全，强制下线
                Member::update(['user_id' => 0], ['id' => $member->id]);
                $this->auth->logout();
                $this->error(__('活动已结束或没有参与权限'), null, Constant::INVALID_ACTIVITY);
            }
            $this->activityId = $activity->id;
            $this->activityData = $activity;
        }
    }
}
